package com.eastmelon.blog.realm;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import com.eastmelon.blog.entity.User;
import com.eastmelon.blog.service.UserService;

public class MyRealm extends AuthorizingRealm{
	@Resource(name="userService")
	private UserService userService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		User user=userService.getUserByUserName(token.getUsername());
		if (user != null) {
			return new SimpleAuthenticationInfo(user.getUsername(), user
					.getPassword(),"admin");
		} else {
			return null;
		}
	}
}
